package FRM;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;

import Corba.EnchereServeurSingleton;
import Encheres.Model.User;
import GestionParametres.GST_Utilisateur;

import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.FlowLayout;
import java.awt.Dialog.ModalExclusionType;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import javax.swing.ImageIcon;

public class FRM_Inscription extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField TXT_Pseudo;
	private JPasswordField PWD_Password;
	private JButton BTN_Valider;
	private JButton BTN_Annuler;

	public FRM_Inscription() {
		setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
		setResizable(false);
		setTitle("Inscription");
		setSize(281, 185);
		setLocationRelativeTo(getParent());

		JPanel PNL_Bouton = new JPanel();
		FlowLayout flowLayout = (FlowLayout) PNL_Bouton.getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);
		getContentPane().add(PNL_Bouton, BorderLayout.SOUTH);

		BTN_Valider = new JButton("Valider");
		BTN_Valider.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				FRM_Inscription.this.Valider();
			}
		});
		PNL_Bouton.add(BTN_Valider);

		BTN_Annuler = new JButton("Annuler");
		BTN_Annuler.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				FRM_Inscription.this.Annuler();
			}
		});
		PNL_Bouton.add(BTN_Annuler);

		JPanel PNL_Form = new JPanel();
		getContentPane().add(PNL_Form, BorderLayout.CENTER);
		PNL_Form
				.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,}));
		
				JLabel lblPseudo = new JLabel("Choisir un pseudo : ");
				PNL_Form.add(lblPseudo, "4, 2, right, default");
		
				TXT_Pseudo = new JTextField();
				PNL_Form.add(TXT_Pseudo, "6, 2, left, default");
				TXT_Pseudo.setColumns(20);
		
				JLabel lblPassword = new JLabel("Choisir un mot de passe : ");
				PNL_Form.add(lblPassword, "4, 4, right, default");
		
				PWD_Password = new JPasswordField();
				PWD_Password.setColumns(20);
				PNL_Form.add(PWD_Password, "6, 4, left, default");
				
				JPanel panel = new JPanel();
				FlowLayout flowLayout_1 = (FlowLayout) panel.getLayout();
				flowLayout_1.setAlignment(FlowLayout.LEFT);
				getContentPane().add(panel, BorderLayout.NORTH);
				
				JLabel lblInscription = new JLabel("Inscription");
				lblInscription.setHorizontalAlignment(SwingConstants.LEFT);
				lblInscription.setIcon(new ImageIcon(FRM_Inscription.class.getResource("/Images/User_48.png")));
				lblInscription.setFont(new Font("Tahoma", Font.BOLD, 14));
				panel.add(lblInscription);
	}

	protected void Annuler() {
		this.setVisible(false);
	}

	protected void Valider() {
		if (champsValid()) {
			// Creation du nouvel utilisateur
			User newUsr = new User();
			newUsr.setPseudo(TXT_Pseudo.getText());
			newUsr.setPassword(String.valueOf(PWD_Password.getPassword()));
			
			// Insertion du nouvel utilisateur en base
			if (GST_Utilisateur.createUtilisateur(newUsr)) {
				javax.swing.JOptionPane.showMessageDialog(null,
						"Utilisateur créé avec succès");
			} else {
				javax.swing.JOptionPane.showMessageDialog(null,
						"Pseudo déjà existant");
			}

			this.setVisible(false);
		}
	}

	private boolean champsValid() {
		boolean valid = true;

		if (TXT_Pseudo.getText() == "") {
			valid = false;
		}

		if (PWD_Password.getPassword().length == 0) {
			valid = false;
		}

		return valid;

	}

}
